<?php

namespace App\Remote\MoreDo;

use App\Models\Invoice\InvoiceHistoryModel;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Validation\ValidationException;
use Illuminate\View\Compilers\BladeCompiler;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class InvoiceRemote extends Remote
{

    /**
     * 数字蓝票
     *
     * @param string $orderId
     * @param string $spxx
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public function applyInvoice(string $orderId, string $spxx)
    {
        return self::customRequestV3(
            "/web/api/request/applyInvoice",
            RequestAlias::METHOD_POST,
            [
                "token"   => AuthRemote::getToken(),
                "invoice" => config("wps.invoice"),
                "orderId" => $orderId,
                "charset" => "GBK",
                "spxx"    => base64_encode(
                    iconv(
                        "UTF-8", "GB2312//IGNORE",
                        $spxx
                    )
                )
            ],
            null,
            null,
            0,
            true
        );
    }

    /**
     * 数字红票
     *
     * @param string $orderId
     * @param string $fphm
     * @param string $fplxdm
     * @param string $hcyydm
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function applyRedInvoice(string $orderId, string $fphm, string $fplxdm, string $hcyydm = "01")
    {
        return self::customRequestV3(
            "/web/api/request/addRedInvoice",
            RequestAlias::METHOD_POST,
            [
                "token"   => AuthRemote::getToken(),
                "invoice" => config("wps.invoice"),
                "orderId" => $orderId,
                "fphm"    => $fphm,
                "fplxdm"  => $fplxdm,
                "hcyydm"  => $hcyydm
            ],
            null,
            null,
            0,
            true
        );
    }

    /**
     * 主动拉取开票信息详情
     *
     * @param string $orderId
     * @param int $redSign
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function getInvoice(string $orderId, int $redSign = 0)
    {
        return self::customRequestV3(
            "/web/api/request/getInvoice",
            RequestAlias::METHOD_POST,
            [
                "token"   => AuthRemote::getToken(),
                "invoice" => config("wps.invoice"),
                "orderId" => $orderId,
                "hcbz"    => $redSign
            ],
            null,
            null,
            0,
            true
        );
    }

    /**
     * 渲染请求xml模板
     *
     * @param InvoiceHistoryModel $invoiceHistoryModel
     * @param array $details
     * @return string
     */
    public static function renderTemplate(
        InvoiceHistoryModel $invoiceHistoryModel,
        array $details
    ): string {
        return BladeCompiler::render(
            file_get_contents(resource_path("/Invoice/ApplyInvoiceTemplate.template")), [
            "invoiceHistoryModel" => $invoiceHistoryModel,
            "details"             => $details
        ]);
    }


}
